package net.ncraft.mc.ezframework.core.ezbatis;

import java.util.HashMap;
import java.util.Map;

public class StringUtil {

    public static Map<Integer, String> replaceBrackets(String sql, Object[] args) {

        Map<Integer, String> sqlList = new HashMap<>();

        if (args == null || args.length == 0) {
            sqlList.put(0, sql);
            return sqlList;
        }

        int index = 1;

        for (int i = 0; i < sql.length(); i++) {
            char c = sql.charAt(i);
            if (c == '#') {
                if (sql.charAt(i + 1) == '{') {
                    int j = i + 2;
                    while (sql.charAt(j) != '}') {
                        j++;
                    }
                    String param = sql.substring(i, j + 1);
                    String paramName = sql.substring(i + 2, j);
                    sqlList.put(index, paramName);
                    index++;
                    sql = sql.replace(param, "?");
                }
            }
        }
        sqlList.put(0, sql);

        return sqlList;
    }

    public static String getGenericClass(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        if (str.indexOf("<") == -1) {
            return null;
        }
        int index = str.indexOf("<");
        if (str.indexOf(">") == -1) {
            return null;
        }
        int index2 = str.indexOf(">");
        if (index2 < index) {
            return null;
        }
        return str.substring(index + 1, index2);
    }


}
